package classLoader;

public class MyClassLoaderTest {

    public static void main(String[] args) {

        MyClassLoaderTest myClassLoaderTest = new MyClassLoaderTest();

//        myClassLoaderTest.fun1();
//        myClassLoaderTest.fun2();
        myClassLoaderTest.fun3();


    }

    /**
     * 自定义类加载器
     */
    public void fun1() {
        // class文件路径为：D:\gitee\mavenProject\src\main\java\classLoader\Demo.class
        MyClassLoader myClassLoader = new MyClassLoader("D:\\gitee\\mavenProject\\src\\main\\java\\classLoader\\");
        try {
            Class clazz = myClassLoader.loadClass("Demo");
            // 加载Demo的类加载器为：classLoader.MyClassLoader@66d3c617   加载器类名：classLoader.MyClassLoader
            System.out.println("加载Demo的类加载器为：" + clazz.getClassLoader()
                    + "   加载器类名：" + clazz.getClassLoader().getClass().getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 类加载器探究
     */
    public void fun2() {
        // 自定义类加载器
        MyClassLoader myClassLoader = new MyClassLoader("D:\\gitee\\mavenProject\\src\\main\\java\\classLoader\\");
        System.out.println(myClassLoader); //classLoader.MyClassLoader@66d3c617
        // 自定义类加载器父类默认为应用程序类加载器
        System.out.println(myClassLoader.getParent()); //sun.misc.Launcher$AppClassLoader@6e0be858

        // 应用程序类加载器
        ClassLoader applicationClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(applicationClassLoader); //sun.misc.Launcher$AppClassLoader@6e0be858

        // 扩展类加载器
        ClassLoader extensionClassLoader = applicationClassLoader.getParent();
        System.out.println(extensionClassLoader); //sun.misc.Launcher$ExtClassLoader@63947c6b

        // 启动类加载器
        ClassLoader bootstrapClassLoader =  extensionClassLoader.getParent();
        System.out.println(bootstrapClassLoader); //null


        try {
            // 核心类用启动类加载器
            ClassLoader classLoader1 = Class.forName("java.lang.String").getClassLoader();
            System.out.println(classLoader1); //null

            // 自定义的类默认使用应用程序类加载器（Demo.class的路径）
            ClassLoader classLoader2 = Class.forName("classLoader.Demo").getClassLoader();
            System.out.println(classLoader2); //sun.misc.Launcher$AppClassLoader@6e0be858

            String[] strArr = new String[10];
            Class strArrClass = strArr.getClass();
            System.out.println(strArrClass); //class [Ljava.lang.String;
            ClassLoader classLoader3 = strArrClass.getClassLoader();
            System.out.println(classLoader3); //null-->String类型用启动类加载器

            int[] intArr = new int[10];
            Class intArrClass = intArr.getClass();
            System.out.println(intArrClass); //class [I
            ClassLoader classLoader4 = intArrClass.getClassLoader();
            System.out.println(classLoader4); //null-->基本数据类型不需要加载器

            Demo[] demoTestArr = new Demo[10];
            Class demoTestArrClass = demoTestArr.getClass();
            System.out.println(demoTestArrClass); //class [LclassLoader.Demo;
            ClassLoader classLoader5 = demoTestArrClass.getClassLoader();
            System.out.println(classLoader5); //sun.misc.Launcher$AppClassLoader@6e0be858

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 同一个类文件用不同的加载器加载是不同的
     */
    public void fun3() {
        MyClassLoader myClassLoader1 = new MyClassLoader("D:\\gitee\\mavenProject\\src\\main\\java\\classLoader\\");
        MyClassLoader myClassLoader2 = new MyClassLoader("D:\\gitee\\mavenProject\\src\\main\\java\\classLoader\\");
        try {
            Class clazz1 = myClassLoader1.loadClass("Demo");
            Class clazz2 = myClassLoader2.loadClass("Demo");
            System.out.println(clazz1 == clazz2); //false
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
